package com.bawei.park2211a.repair

import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.ReqairEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.park2211a.R
import com.bawei.park2211a.adapter.ReqairAdapter
import com.bawei.park2211a.databinding.ActivityRepairBinding
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/repair/RepairActivity")
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {
    private lateinit var repairAdapter: ReqairAdapter
    private val repairList = mutableListOf<ReqairEntity>()
    private var isSort = true
    private var index = 0
    override fun initView() {
        viewModel.reqair()
        repairAdapter = ReqairAdapter()
        binding.rec.adapter = repairAdapter
        binding.rec.layoutManager = LinearLayoutManager(this)
        binding.repaorSort.setOnClickListener {
            if (isSort) {
                repairAdapter.data.sortBy {
                    it.repairTime
                }
            } else {
                repairAdapter.data.sortByDescending {
                    it.repairTime
                }
            }

            repairAdapter.notifyDataSetChanged()
            isSort = !isSort
        }
        binding.repaorScreen.setOnClickListener {
            val list = arrayListOf("全部", "待处理", "维修中", "已完成")

            val build = OptionsPickerBuilder(this) { position1, _, _, _ ->
                run {
                    binding.repaorScreen.text = list[position1]
                    repairAdapter.data.clear()
                    when (list[position1]) {
                        "全部" -> {
                            repairAdapter.data.addAll(repairList)
                        }
                        "待处理" -> {
                            repairList.forEach {
                                if (it.processingStatus == "待处理") {
                                    repairAdapter.data.add(it)
                                }
                            }
                        }
                        "维修中" -> {
                            repairList.forEach {
                                if (it.processingStatus == "维修中") {
                                    repairAdapter.data.add(it)
                                }
                            }
                        }
                        "已完成" -> {
                            repairList.forEach {
                                if (it.processingStatus == "已完成") {
                                    repairAdapter.data.add(it)
                                }
                            }
                        }
                        else -> {
                            repairAdapter.data.addAll(repairList)
                        }

                    }
                    repairAdapter.notifyDataSetChanged()
                }
            }.build<String>()
            build.setPicker(list)
            build.show()
        }
        binding.repairAdd.setOnClickListener {
            ARouter.getInstance().build("/app/repair/AddRepairActivity").navigation()
        }
        binding.repairSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                repairAdapter.data.clear()
                repairList.forEach {
                    if (it.orderNumber.contains(query!!)) {
                        repairAdapter.data.add(it)
                    }
                    repairAdapter.notifyDataSetChanged()
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!!.isEmpty()) {
                    repairAdapter.data.addAll(repairList)
                    repairAdapter.notifyDataSetChanged()
                }
                return true
            }
        })
        repairAdapter.addChildClickViewIds(R.id.reqair_state)
        repairAdapter.setOnItemChildClickListener { _, _, position ->
            if (repairAdapter.data[position].processingStatus != "已完成") {
                val list = arrayListOf("维修中", "已完成")
                val build = OptionsPickerBuilder(this) { position1, _, _, _ ->
                    run {
                        index = position
                        repairAdapter.data[position].processingStatus = list[position1]
                        viewModel.updataRepair(
                            repairAdapter.data[position].id,
                            repairAdapter.data[position]
                        )
                    }
                }.build<String>()
                build.setPicker(list)
                build.show()
            }
        }
    }

    override fun initData() {
        viewModel.reqairSuccess.observe(this) {
            repairList.clear()
            repairList.addAll(it)
            repairAdapter.data.clear()
            repairAdapter.data.addAll(it)
            repairAdapter.notifyDataSetChanged()
        }
        viewModel.updatareqairSuccess.observe(this) {
            repairAdapter.notifyItemChanged(index)
        }
    }
}